package com.jewaton.auth.commons.util;

import java.math.BigDecimal;

/**
 * 数字工具类
 *
 */
public class NumberUtil {

    /**
     * 缺省保留2位小数点
     */
    private static final int DEF_DIV_SCALE = 2;
    
    /**
     * 判断字符串是否为数字
     * @param num
     * @return
     */
    public static boolean isNumberic(String num) {
        return (null == num || num.length() <= 0 || num.matches("\\d{1,}")) ? true : false;
    }

    /**
     * 判断字符串数组中每个元素是否为数字
     * @param nums
     * @return
     */
    public static boolean isNumberic(String[] nums){
    	if (nums == null || nums.length == 0){
    		return false;
    	}
    	for (int i = 0; i < nums.length; i ++){
    		if (!isNumberic(nums[i])){
    			return false;
    		}
    	}
    	return true;
    }
    
    /**
     * 将字符串数组转换为数字数组
     * @param nums 被转换的字符串数组
     * @return 字符串中元素不为数字字符串则返回null
     */
    public static Long[] convertToNumber(String[] nums){
    	Long[] num = null;
    	if (nums.length > 0 && isNumberic(nums)){
    		num = new Long[nums.length];
    		for (int i = 0; i < nums.length; i ++){
    			num[i] = Long.valueOf(nums[i]);
    		}
    	}
    	return num;
    }
    
    
    /**
     * 将字符串数组转换为数字数组
     * @param nums 被转换的字符串数组
     * @return 字符串中元素不为数字字符串则返回null
     */
    public static Integer[] convertToInteger(String[] nums){
    	Integer[] num = null;
    	if (nums.length > 0 && isNumberic(nums)){
    		num = new Integer[nums.length];
    		for (int i = 0; i < nums.length; i ++){
    			num[i] = Integer.valueOf(nums[i]);
    		}
    	}
    	return num;
    }
    
    
    /**
     * * 两个Double数相加 *
     * 
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.add(b2).doubleValue());
    }

    /**
     * * 两个Double数相减 *
     * 
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double sub(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.subtract(b2).doubleValue());
    }

    /**
     * * 两个Double数相乘 *
     * 
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double mul(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.multiply(b2).doubleValue());
    }

    /**
     * * 两个Double数相除 *
     * 
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double div(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1
                .divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP)
                .doubleValue());
    }

    /**
     * * 两个Double数相除，并保留scale位小数 *
     * 
     * @param v1 *
     * @param v2 *
     * @param scale *
     * @return Double
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP)
                .doubleValue());
    }
    
    /**
     * @param v1
     * @return 返回指定Double的负数
     */
    public static Double neg(Double v1) {
        return sub(new Double(0),v1);
    }
    
    public static void main(String[] args) {
        System.out.println(div(6D, 9D));
        System.out.println(div(6D, 9D, 3));
        System.out.println(convertToNumber(new String[]{"1","23","4545"}));
    }
    
}